<?php 
class Live_match{
   		
	function __construct()
	{
		$this->user_id = $_SESSION['admin_id'];		
	}

 	
	function getMatchInningInfo($match_id)
	{
		global $db;
		$sql = "select match_inning_id,match_id,batting_team_id,bowling_team_id , getTeamNameByTournamentTeamIdl(batting_team_id) as team_name,
				getTeamShortNameByTeamId(batting_team_id) as  team_short_name, total_runs , total_wickets , total_balls ,is_target , 
				target_overs , target_score
				from pcb_match_inning 
				where match_id = '".$match_id ."' order by match_inning_id asc";
		$result = $db->fetch_all($sql);
		return $result;
	}
	
    function getMatchInfo($match_id){
		global $db;
		$sql = "select * from pcb_match where match_id = '".$match_id ."'";
		$result = $db->first($sql);
		return $result;
		
	}
	
	function loadScoreBoardRuningInning($post , $match_inning_info , $match_info){
		
			global $config  ;		
			 
 			//sizeof($match_inning_info);
			$type = $post['type'];
			$toss_decision   = $match_info['toss_decision'];
			$is_player_added = $match_info['is_player_added'];
			
	
		    $total_inning = ((int)sizeof($match_inning_info) - 1)  ; 
			
			if($type == 'all_innings'){
					$match_inning_id   = $match_inning_info[$total_inning]['match_inning_id'];
					$match_id		   = $match_inning_info[$total_inning]['match_id'];
					$batting_team_id   = $match_inning_info[$total_inning]['batting_team_id'];
					$bowling_team_id   = $match_inning_info[$total_inning]['bowling_team_id'];
			}else{
					$match_inning_id   = $post['match_inning_id'];
					$match_id		   = $post['match_id'];
					$batting_team_id   = $post['batting_team_id'];
					$bowling_team_id   = $post['bowling_team_id'];
			}

		$match_type = $post['match_type_code'];
 		if(($toss_decision == 'pending') || ($is_player_added == "NO")) {
		
			
				$file_name_ = $match_type.'_match_'.$match_id.'_info.xml';
				$file_path_ = $config['baseurl'].'/live_comentary_xml/'.$file_name_; 
				
				$xml_string_ = parseXMLFileTOString($file_path_);
				$doc_		 = new DOMDocument();
				
				$doc_->loadHTML($xml_string_);
				
				$team_name_1 = $doc_->getElementsByTagName("team_name_1")->item(0)->nodeValue ; 
				stemplate::assign("team_name_1", $team_name_1);
				
				$team_name_2 = $doc_->getElementsByTagName("team_name_2")->item(0)->nodeValue;
				stemplate::assign("team_name_2", $team_name_2 );
		
				stemplate::assign("team_logo_1", $doc_->getElementsByTagName("team_logo_1")->item(0)->nodeValue);
				stemplate::assign("team_logo_2", $doc_->getElementsByTagName("team_logo_2")->item(0)->nodeValue);
 		
				$match_team_1_ID =  $doc_->getElementsByTagName("team_id_1")->item(0)->nodeValue;
				$match_team_2_ID =  $doc_->getElementsByTagName("team_id_2")->item(0)->nodeValue;
				
				$players_1 = $doc_->getElementsByTagName("players_".$match_team_1_ID."");	 
				foreach($players_1 as $player_info){   
					
						$team_1_players[$counter]['player_id'] = $player_info->getAttribute('player_id'); 
						$team_1_players[$counter]['player_name'] = $player_info->getAttribute('player_name'); 
						$team_1_players[$counter]['role_id'] = $player_info->getAttribute('role_id'); 
						$counter++;
						
				}//$blowers
		
				$players_2 = $doc_->getElementsByTagName("players_".$match_team_2_ID."");	 
				foreach($players_2 as $player_info){   
					
						$team_2_players[$counter]['player_id'] = $player_info->getAttribute('player_id'); 
						$team_2_players[$counter]['player_name'] = $player_info->getAttribute('player_name'); 
						$team_2_players[$counter]['role_id'] = $player_info->getAttribute('role_id'); 
						$counter++;
						
				}//$blowers
				
				$toss_decision = $doc_->getElementsByTagName("toss_decision")->item(0)->nodeValue ; 
				stemplate::assign("toss_decision", $toss_decision);
				
 				stemplate::assign("team_1_players", $team_1_players);
				stemplate::assign("team_2_players", $team_2_players);
 				stemplate::display("live_match_widget/tournament_squad.tpl");
		
		
		}	else{
					
			//T20_match_2_2_26_scoreboard.xml
			$file_name = $match_type.'_match_'.$match_id.'_'.$match_inning_id.'_'.$batting_team_id.'_scoreboard.xml';
			$file_path = $config['baseurl'].'/live_comentary_xml/'.$file_name; 
			
			$xml_string = parseXMLFileTOString($file_path);
			
			$doc		= new DOMDocument();
			$doc->loadHTML($xml_string);
			
			$batsmans = $doc->getElementsByTagName("batsman_score");	
			$counter  = 0 ; 
			 
			//$team_1_ID =  $doc->getElementsByTagName("batsman")->item(0)->getAttribute('bating_team_id');
			//$team_2_ID =  $doc->getElementsByTagName("blower")->item(0)->getAttribute('bowling_team_id');
			 
 			foreach($batsmans as $batsman_data){   
				
					$batsman_info[$counter]['player_id'] = $batsman_data->getAttribute('player_id'); 
					$batsman_info[$counter]['batsman_name'] = $batsman_data->getAttribute('batsman_name'); 
					$batsman_info[$counter]['dismissal_type_id'] = $batsman_data->getAttribute('dismissal_type_id'); 
					$batsman_info[$counter]['roll_code'] = $batsman_data->getAttribute('roll_code'); 
					$batsman_info[$counter]['dismissal_bolwer_name'] = $batsman_data->getAttribute('dismissal_bolwer_name'); 
					$batsman_info[$counter]['fielder'] = $batsman_data->getAttribute('fielder'); 
					$batsman_info[$counter]['runs'] = $batsman_data->getAttribute('runs'); 
					$batsman_info[$counter]['balls'] = $batsman_data->getAttribute('balls'); 
					$batsman_info[$counter]['fours'] = $batsman_data->getAttribute('fours'); 
					$batsman_info[$counter]['sixes'] = $batsman_data->getAttribute('sixes'); 
					$batsman_info[$counter]['strike_rate'] = $batsman_data->getAttribute('strike_rate'); 
					$batsman_info[$counter]['onstrike'] = $batsman_data->getAttribute('onstrike'); 
					
$batsman_info[$counter]['dismissal_type'] = $this->getDisMissalStatus($batsman_info[$counter]['dismissal_type_id']  , $batsman_info[$counter]['dismissal_bolwer_name'] , $batsman_info[$counter]['fielder']); 
					
					$counter++;
					
			}//$batsman
 			$_extrascore = $doc->getElementsByTagName("extrascore");	
			foreach($_extrascore as $extrascore_data){   
				
					$extrascore_info['b']   = $extrascore_data->getAttribute('b'); 
					$extrascore_info['lb'] = $extrascore_data->getAttribute('lb'); 
					$extrascore_info['nb'] = $extrascore_data->getAttribute('nb'); 
					$extrascore_info['wd'] = $extrascore_data->getAttribute('wd'); 
					$extrascore_info['p'] = $extrascore_data->getAttribute('p'); 
					
					$extrascore_info['total_score'] = $extrascore_data->getAttribute('total_score'); 
					$extrascore_info['total_balls'] = $extrascore_data->getAttribute('total_balls'); 
					$extrascore_info['player_out'] = $extrascore_data->getAttribute('player_out'); 
					
					list($overs, $ball) = explode(".",$extrascore_info['total_balls']);
					$tatal_balls = round($overs + ($ball/6), 2) ;
					$strike_rate =  round(( $extrascore_info['total_score'] / $tatal_balls ) , 2); 
					$extrascore_info['strike_rate'] = $strike_rate ; 
 					
			}//$batsman
			
  			$extrascore_info['total_extra'] = $extrascore_info['b']+$extrascore_info['lb']+ $extrascore_info['nb']+$extrascore_info['wd']+$extrascore_info['p'];
			
			
			$blowers = $doc->getElementsByTagName("blower_score");	
			$counter  = 0 ; 
			 
			foreach($blowers as $blower_data){   
				
					$blower_info[$counter]['player_id'] = $blower_data->getAttribute('player_id'); 
					$blower_info[$counter]['blower_name'] = $blower_data->getAttribute('blower_name'); 
					$blower_info[$counter]['overs'] = $this->formatBall($blower_data->getAttribute('balls')); 
					$blower_info[$counter]['maiden'] = $blower_data->getAttribute('maiden'); 
					$blower_info[$counter]['runs'] = $blower_data->getAttribute('runs'); 
					$blower_info[$counter]['fours'] = $blower_data->getAttribute('fours'); 
					$blower_info[$counter]['sixes'] = $blower_data->getAttribute('sixes'); 
					$blower_info[$counter]['wickets'] = $blower_data->getAttribute('wickets'); 
					$blower_info[$counter]['wide_balls'] = $blower_data->getAttribute('wide_balls'); 
					$blower_info[$counter]['no_balls'] = $blower_data->getAttribute('no_balls'); 
					$blower_info[$counter]['run_rate'] = $blower_data->getAttribute('run_rate'); 
					$blower_info[$counter]['balls'] = $blower_data->getAttribute('balls'); 
					$blower_info[$counter]['onblowing'] = $blower_data->getAttribute('onblowing'); 
					$counter++;
					
			}//$blowers
 			
			
			$fall_of_wicket = $doc->getElementsByTagName("fall_of_wicket");	
			$counter  = 0 ; 
			 
			foreach($fall_of_wicket as $wicket_data){   
				
				if ( $wicket_data->getAttribute('dismiss_batsman_id') > 0 )
				{
					$wicket_info[$counter]['dismiss_batsman_id'] = $wicket_data->getAttribute('dismiss_batsman_id');
					$wicket_info[$counter]['dismissal_player_name'] = $wicket_data->getAttribute('dismissal_player_name'); 
					$wicket_info[$counter]['partnership_runs'] = $wicket_data->getAttribute('partnership_runs'); 
					$wicket_info[$counter]['overs'] = $wicket_data->getAttribute('overs') ; 
					$counter++;
				}
					
			}//$blowers
			
	
			

			$file_name_ = $match_type.'_match_'.$match_id.'_info.xml';
			$file_path_ = $config['baseurl'].'/live_comentary_xml/'.$file_name_; 
			
			$xml_string_ = parseXMLFileTOString($file_path_);
			$doc_		 = new DOMDocument();
			
			$doc_->loadHTML($xml_string_);
			
			$team_name_1 = $doc_->getElementsByTagName("team_name_1")->item(0)->nodeValue ; 
			stemplate::assign("team_name_1", $team_name_1);
			
			$team_name_2 = $doc_->getElementsByTagName("team_name_2")->item(0)->nodeValue;
			stemplate::assign("team_name_2", $team_name_2 );
	
			stemplate::assign("team_logo_1", $doc_->getElementsByTagName("team_logo_1")->item(0)->nodeValue);
			stemplate::assign("team_logo_2", $doc_->getElementsByTagName("team_logo_2")->item(0)->nodeValue);
			stemplate::assign("toss_decision_string", $doc_->getElementsByTagName("toss_decision_string")->item(0)->nodeValue);
			stemplate::assign("match_overs", $doc_->getElementsByTagName("match_overs")->item(0)->nodeValue);
	
			$match_team_1_ID =  $doc_->getElementsByTagName("team_players_1")->item(0)->getAttribute('team_id_1');
			$match_team_2_ID =  $doc_->getElementsByTagName("team_players_2")->item(0)->getAttribute('team_id_2');
			
			if($match_team_1_ID == $bowling_team_id){
				$bowling_tean_name = $team_name_1 ; 
			}elseif($match_team_2_ID == $bowling_team_id){
				$bowling_tean_name = $team_name_2 ; 	
			}
			
			$players = $doc_->getElementsByTagName("players_".$bowling_team_id."");	 
			foreach($players as $player_info){   
				
					$bowling_team_players[$counter]['player_id'] = $player_info->getAttribute('player_id'); 
					$bowling_team_players[$counter]['player_name'] = $player_info->getAttribute('player_name'); 
 					$bowling_team_players[$counter]['role_id'] = $player_info->getAttribute('role_id'); 
					$counter++;
					
			}//$blowers
   	
			$toss_decision = $doc_->getElementsByTagName("toss_decision")->item(0)->nodeValue ; 
			stemplate::assign("toss_decision", $toss_decision);
			
			stemplate::assign("bowling_tean_name", $bowling_tean_name);
 			stemplate::assign("bowling_team_players", $bowling_team_players);
			
 			stemplate::assign("match_inning_infos", $match_inning_infos);
			stemplate::assign("total_innings", sizeof($match_inning_infos));
  			stemplate::assign("batsman_info", $batsman_info);
			stemplate::assign("extrascore_info", $extrascore_info);
			stemplate::assign("blower_info", $blower_info);
			stemplate::assign("wickets_info", $wicket_info);
			stemplate::assign("total_wickets", sizeof($wicket_info));
			stemplate::assign("match_inning_id", $match_inning_id);
			stemplate::assign("overs", $overs);
			stemplate::display("live_match_widget/scoreboard.tpl");
		
		}			
	}//loadScoreBoardRuningInning	
	
	function getDisMissalStatus($dismissal_type_id , $bolwer_name , $fielder){
			global $dismissal_array;
			$dismissal_name = $dismissal_array[$dismissal_type_id];
			
			if($dismissal_type_id == 1)	{ // catch
				$dismissal_section = "$dismissal_name $fielder b $bolwer_name";	
			}elseif($dismissal_type_id == 2)	{ // bowled
				$dismissal_section = "$dismissal_name $bolwer_name";	
			}elseif($dismissal_type_id == 3)	{ // catch and bowl
				$dismissal_section = "$dismissal_name $fielder ";	
			}elseif($dismissal_type_id == 4)	{ // lbw
				$dismissal_section = "$dismissal_name $bolwer_name";	
			}elseif($dismissal_type_id == 5)	{ // stump
				$dismissal_section = "$dismissal_name $fielder b $bolwer_name";	
			}elseif($dismissal_type_id == 6)	{ // catch behind
				$dismissal_section = "$dismissal_name $fielder b $bolwer_name";	
			}elseif($dismissal_type_id == 7)	{ // run out
				$dismissal_section = "$dismissal_name $fielder";	
			}elseif($dismissal_type_id == 10)	{ // hit the ball twice
				$dismissal_section = "$dismissal_name";	
			}elseif($dismissal_type_id == 11)	{ // retired hurt 
				$dismissal_section = "$dismissal_name";	
			}elseif($dismissal_type_id == 12)	{ // hit wicket
				$dismissal_section = "$dismissal_name $bolwer_name";	
 			}elseif($dismissal_type_id == 13)	{ // hit wicket
				$dismissal_section = "$dismissal_name";	
 			}elseif($dismissal_type_id == 14)	{ // Handled the ball
				$dismissal_section = "$dismissal_name";	
 			}elseif($dismissal_type_id == 15)	{ // Obstructing the field
				$dismissal_section = "$dismissal_name";	
 			}elseif($dismissal_type_id == 16)	{ // Timed Out
				$dismissal_section = "$dismissal_name";	
			}elseif($dismissal_type_id == 17)	{ // Absent Hurt
				$dismissal_section = "$dismissal_name";	
			}elseif($dismissal_type_id == 9)	{ // run out
				$dismissal_section = "not out";	
			}else{
				$dismissal_section = "To bat";		
			}
		return $dismissal_section;
	}//getDisMissalStatus
	
	function getDismissalLabel(){
		global $db;
		$sql = "select * from pcb_dismissal_types";
		$result = $db->fetch_all($sql);
		
		foreach($result as $row){
			$array[$row['dismissal_type_id']] = $row['dismissal_short_name'];
		}
		return $array;
	}
	
	
	function loadMatchCommentary($post){
		global $config;
		$match_id 		 = $post['match_id'];
		$match_type_code =  $post['match_type_code'];
		
		$file_name = $match_type_code.'_match_'.$match_id.'_live_commentary.xml';
  		$file_path = $config['baseurl'].'/live_comentary_xml/'.$file_name; 
		
		$xml_string = parseXMLFileTOString($file_path);
		
		$doc = new DOMDocument();
	    $doc->loadHTML($xml_string);
		
		$batting_team_name =  $doc->getElementsByTagName( "batting_team_name" )->item(0)->nodeValue;
		$match_score	   =  $doc->getElementsByTagName( "match_score" )->item(0)->nodeValue;
		$overs			   =  $doc->getElementsByTagName( "overs" )->item(0)->nodeValue;
		$player_out 	   =  $doc->getElementsByTagName( "player_out" )->item(0)->nodeValue;
					
		 
  		$commentaries = $doc->getElementsByTagName("ball_by_ball_commentary");
		
 		$live_commentary_string = '<div  class="css-scrollbar">';
		//$live_commentary_string .= $batting_team_name." ( ".$match_score." ) ".$overs." Over";
		
		$live_commentary_string .= '<div class="ballbyball"><ul>';
		$counter = 0 ; 
		
		foreach( $commentaries as $commentary ){
				$current_over_scores	     =  $commentary->getElementsByTagName( "current_over_scores" )->item(0)->nodeValue;
				$over_no	     =  $commentary->getElementsByTagName( "over_no" )->item(0)->nodeValue;
				$over		     =  $commentary->getElementsByTagName( "over" )->item(0)->nodeValue;	
				$batsman_name    =  $commentary->getElementsByTagName( "batsman_name" )->item(0)->nodeValue;	
				$bowler_name     =  $commentary->getElementsByTagName( "bowler_name" )->item(0)->nodeValue;	
				$dismissal_type_id  =  trim($commentary->getElementsByTagName( "out" )->item(0)->nodeValue);
 				$fielder 		 =  trim($commentary->getElementsByTagName( "fielder" )->item(0)->nodeValue);	
				$runs		     =  $commentary->getElementsByTagName( "runs" )->item(0)->nodeValue;
				$ball_type		 =  $commentary->getElementsByTagName( "ball_type" )->item(0)->nodeValue;
				$extra_type		 =  $commentary->getElementsByTagName( "extra_type" )->item(0)->nodeValue;	
				$commentary_type =  $commentary->getElementsByTagName( "commentary_type" )->item(0)->nodeValue;	
				$commentary      =  $commentary->getElementsByTagName( "commentary" )->item(0)->nodeValue;
				$commentary 	 = str_replace("\n","<br/>",$commentary );
 				$out 			 = $this->getDisMissalStatus($dismissal_type_id , $bowler_name , $fielder);	
				
  				 if($counter++%2== 0){
					$class =	'class="even"'; 
				 }else{
					$class =	'';  
				 }
				 
				if($commentary_type == 'information'){
				
					$live_commentary_string .='<li style="padding:10px;">';
					$live_commentary_string .= ' <div class="noticebyb">'.$commentary.'</div>'; 
					$live_commentary_string .='<div class="clear"></div></li>'; 
					
				}elseif($commentary_type == 'over_stats'){	
						
						$live_commentary_string .='<li><div id="endofover"><table border="0" cellspacing="0" cellpadding="0"><tr>';
						
 						$match_stats = $doc->getElementsByTagName("match_stats_".$over_no."_over_end")->item(0);	
						
						$batting_team_name =  $match_stats->getAttribute('batting_team_name'); 
 				   	    $total_over   	   =  $match_stats->getAttribute('total_over'); 
						$total_scores 	   =  $match_stats->getAttribute('total_scores'); 
						$total_out    	   =  $match_stats->getAttribute('total_out'); 
  						//$live_commentary_string .='<td colspan="2"><strong>End of over '.$total_over.'</strong> ('.$current_over_scores.' runs) &nbsp;&nbsp; <span><strong>'.$batting_team_name.'</strong> </span> '.$total_scores.'/'.$total_out.'</td></tr><tr class="endoverdetail"><td><table  border="0" cellspacing="0" cellpadding="2">';
						$live_commentary_string .='<td colspan="2"><strong>End of over '.$total_over.'</strong> &nbsp;&nbsp; <span><strong>'.$batting_team_name.'</strong> </span> '.$total_scores.'/'.$total_out.'</td></tr><tr class="endoverdetail"><td><table  border="0" cellspacing="0" cellpadding="2">';						
						
						$batsman_stats = $doc->getElementsByTagName("batsman_".$over_no."_over_stats");
						foreach( $batsman_stats as $batsman_data ){
							
								$batsman_name	 =  $batsman_data->getAttribute('batsman_name');
								$battingp_scores =  $batsman_data->getAttribute('battingp_scores');
								$battingp_balls  =  $batsman_data->getAttribute('battingp_balls');
								$battingp_four 	 =  $batsman_data->getAttribute('battingp_four'); 
								$battingp_six 	 =  $batsman_data->getAttribute('battingp_six'); 
								
								
								$live_commentary_string .='<tr class="endoverdetail"  valign="top"><td><span><strong>'.$batsman_name.'</strong></span></td><td><span>'.$battingp_scores.'* &nbsp;('.$battingp_balls.'b '.(($battingp_four>0)? $battingp_four."x4":"").' '.(($battingp_six>0)?$battingp_six."x6":"").')</span></td></tr>';
						}//foreach batsman 
						
						$live_commentary_string .='</table></td><td  valign="top"><table border="0" cellspacing="0" cellpadding="2">';
						
						$bowler_stats = $doc->getElementsByTagName("bowler_".$over_no."_over_stats");
						foreach( $bowler_stats as $bowler_data ){

								$bowler_name	 =  $bowler_data->getAttribute('bowler_name');
								$bowler_overs    =  $bowler_data->getAttribute('bowler_overs');
								$bowlingp_maidan =  $bowler_data->getAttribute('bowlingp_maidan');
								$bowlingp_runs 	 =  $bowler_data->getAttribute('bowlingp_runs'); 
								$bowlingp_outs 	 =  $bowler_data->getAttribute('bowlingp_outs'); 

								$live_commentary_string .='<tr class="endoverdetail"  valign="top"><td><span><strong>'.$bowler_name.'</strong></span></td><td><span>'.$bowler_overs.' - '.$bowlingp_maidan.' - '.$bowlingp_runs.' - '.$bowlingp_outs.'</span></td></tr>';

						}
 						
						$live_commentary_string .='</table></td></table></div></li>';
						
						
				}elseif($commentary_type == 'normal'){	
				
					$live_commentary_string .='<li ><div class="balnum">'.$over.'</div>';
					$live_commentary_string .='<div class="b_detail"> <div class="cstatus">'.$bowler_name.' to '.$batsman_name.'';
					 
					$live_commentary_string .='&nbsp;<span class="runs">';
					
					if($out != "To bat"){  
						if(trim($out) == "retired hurt"){
							$live_commentary_string .="&nbsp;".$out; 
						}else{
							$live_commentary_string .='<font color="red">OUT</font>&nbsp;'. $out; 
						}
						
					}else{
 						if($ball_type == "wide_ball"){
							$live_commentary_string .= ' Wide '; 	
						}elseif($ball_type == "no_ball"){
							$live_commentary_string .= 'No Ball '; 	
						}elseif($extra_type == "leg_bye"){
							$live_commentary_string .='Leg Byes '; 	
						}elseif($extra_type == "bye"){
							$live_commentary_string .='Byes '; 	
						}
						
						$live_commentary_string .=$runs.' run(s) '; 	
					}
					
					$live_commentary_string .='</span></div> <div class="baldet">'.$commentary.'</div>'; 
					$live_commentary_string .='</div><div class="clear"></div>'; 
					
				
				}
		}//IF
				$live_commentary_string .= '</ul></div></div><script>
		 	  
		jQuery(".css-scrollbar").scrollbar();
	  
 </script>';	
 		
			echo $live_commentary_string  ; 
	}//loadMatchCommentary
	
	function formatBall($total_balls){
		if($total_balls>0){
			return floor($total_balls/6).".".($total_balls%6) ; 
						
		}else{
			return  0 ; 	
		}
	}
	
	
	function getWeather($post){
			global $config ;
 			$match_city = $post['match_city'];
			$match_country = $post['match_country'];

 			$url = "http://www.google.com/ig/api?weather=".$match_city.",".$match_country."";
			$weather_data = parseXMLFileTOString($url);
 			$doc = new DOMDocument();
			$doc->loadHTML($weather_data);
 			$condition = $doc->getElementsByTagName("condition")->item(0)->getAttribute('data'); 
			$humidity  = $doc->getElementsByTagName("humidity")->item(0)->getAttribute('data');
			$temp_f  = $doc->getElementsByTagName("temp_f")->item(0)->getAttribute('data'); 
			$temp_c  = $doc->getElementsByTagName("temp_c")->item(0)->getAttribute('data'); 
 			$icon   =  '<img src="http://www.google.com/' .$doc->getElementsByTagName("icon")->item(0)->getAttribute('data'). '" width="40" height="40"/>';
			
			$wind_condition  = $doc->getElementsByTagName("wind_condition")->item(0)->getAttribute('data'); 

 			stemplate::assign("wind_condition", $wind_condition);
			stemplate::assign("location", $location);
			stemplate::assign("temp", $temp_c);
			stemplate::assign("icon", $icon);
			stemplate::assign("condition", $condition);
			stemplate::assign("humidity", $humidity);
			stemplate::assign("current_date", date("m/d/Y",time()));
			stemplate::display("live_match_widget/weather.tpl");
								 
 	}//function getWeather
	
	
	function getCommentaryListingByMatchId_CID($match_id , $match_inning_id , $match_type){

		global $db, $pager, $config;
		
		
		$pager = new Paginator();
		$commentary_table = getTableName($match_type  , 'commentary');
		$config['page_limit'] = 30;
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}
 		 
		$sql = "SELECT SQL_CALC_FOUND_ROWS match_commentary_id,match_id,batsman_id,getPlayerNameById( batsman_id) as batsman_name , bowler_id,
				getPlayerNameById( bowler_id) as bowler_name,
				match_inning_id,over,ball_no,ball_type,extra_type,six_four,run,commentary,commentary_type,dismissal_type_id ,
				match_inning_id,getPlayerNameById (dismissal_player_involve) as fielder
 				FROM ".$commentary_table." p  
 				where match_id = '".$match_id."' AND match_inning_id = '".$match_inning_id."'
 				order by match_commentary_id desc $limit";
 		$result =  $db->fetch_all($sql);
		
  		$counter = $db->queryCount(); 
		$pager->items_total = $counter[0];
 		
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		
		//echo '<input type="hidden" name="test" value="'.$pager->default_ipp.'-'.$pager->items_total.'" />';
		
		$pager->paginate($path);
		foreach( $result as &$row){
				
				
				$row['out'] = $this->getDisMissalStatus( $row['dismissal_type_id'] , $row['bowler_name'] , $row['fielder'] );	
				
				
				$ball_no   = $row['ball_no'] ; 
				$ball_type = $row['ball_type'] ; 
				$over	   = $row['over'] ; 
				
				if(($ball_no == 6 ) && ($ball_type == "normal" )){
						
						require_once ($config['basedir']."/classes/live_score_rss.class.php");
						
						$obj_score_rss = new Live_score_rss();
						
						$row['over_stats'] = $obj_score_rss->getOverEndStats( $row['match_commentary_id'] ); 
						
				}
				
		}
 		
		 // varDump($result);
		return $result;
		
	}//getCommentaryListingByMatchId_CID
	
	
	function getTName_MatchNameByMatchID($match_id){
		
		 global $db;
			
		$sql = "SELECT ts.match_name,ts.match_start_date,ts.tournament_schedule_id,t.tournament_name,t.tournament_id,m.match_id
				FROM pcb_tournament_schedule ts , pcb_tournament t  , pcb_match m 
				where ts.tournament_id = t.tournament_id AND  ts.tournament_schedule_id = m.	tournament_schedule_id
				and m.match_id = '".$match_id."'";	
		$result = $db->first($sql);
		return $result;
	}
	
	function getMatchInningScoreResult($post , $data , $match_info){
			
		$match_id		 	  = $post['match_id'];
		$match_type_code 	  = $post['match_type'];
		$toss_decision_string = $post['toss_decision_string'];
		$toss_decision		  = $match_info['toss_decision'];
		if($toss_decision == 'pending'){ return false; } 
		if($match_type_code == 'TEST')
		{  
			if(sizeof($data) == 2)	// 2nd inning is going on 
			{
				$batting_team_score = $data[0]['total_runs'] ; 
				$bowling_team_score =  $data[1]['total_runs'] ; 
				$score_diff  = ($bowling_team_score -  $batting_team_score) ;
				if( $score_diff  > 0)
				{  
					$score_need_win = $data[1]['team_name']." lead by ". abs( $score_diff );	
				}else{
					$score_need_win = $data[1]['team_name']." trail by ".abs($score_diff);	
				}
			}
			elseif(sizeof($data) == 3)	// 3rd inning is going 
			{  
				$batting_team_id_0 = $data[0]['batting_team_id'] ; 
				$batting_team_id_1 = $data[1]['batting_team_id'] ; 
				$batting_team_id_2 = $data[2]['batting_team_id'] ; 
				$follow_on = false;
				
				if($batting_team_id_0 == $batting_team_id_2 )	// not follow on
				{
					$batting_team_score  = $data[0]['total_runs'] + $data[2]['total_runs'] ; 
					$bowling_team_score  = $data[1]['total_runs'] ; 
					$score_diff = $bowling_team_score - $batting_team_score;
				}
				else	// in case of follow on 
				{
					$follow_on = true;
					$batting_team_score  = $data[0]['total_runs'] + $data[1]['total_runs'] ; 
					$bowling_team_score  = $data[2]['total_runs'] ; 
					$score_diff = $bowling_team_score - $batting_team_score;
				}
				
				if ( $score_diff > 0 )
				{
					
					$score_need_win = $data[2]['team_name']." trail by ".$score_diff." runs";	
				}
				else
				{	 
					$score_need_win = $data[2]['team_name']." lead by ".abs($score_diff)." runs";	
				}
			}
			elseif(sizeof($data) == 4)	// 4rd inning is going 
			{  
				$batting_team_id_0 = $data[0]['batting_team_id'] ; // 7 
				$batting_team_id_1 = $data[1]['batting_team_id'] ; // 6
				$batting_team_id_2 = $data[2]['batting_team_id'] ; // 7
				$follow_on = false;
				
				if($batting_team_id_0 == $batting_team_id_2 )	// not follow on
				{
					$batting_team_score  = $data[0]['total_runs'] + $data[2]['total_runs'] ; 
					$bowling_team_score  = $data[1]['total_runs'] + $data[3]['total_runs']; 
					$score_diff = $bowling_team_score - $batting_team_score;
				}
				else	// in case of follow on 
				{
					$follow_on = true;
					$batting_team_score  = $data[0]['total_runs'] + $data[1]['total_runs'] ; 
					$bowling_team_score  = $data[2]['total_runs'] + $data[3]['total_runs']; 
					$score_diff = $bowling_team_score - $batting_team_score;
				}
				
				if ( $score_diff > 0 )
				{
					$score_need_win = $data[3]['team_name']." lead by ".abs($score_diff)." runs";	
					
				}
				else
				{
					$score_need_win= $data[3]['team_name']." trail by ".abs($score_diff)." runs";		
				}
			}
			
		}
		else	// Math type other than TEST i.e  not equal "TEST" ( ODI and T20 )
		{
			if(sizeof($data) == 2)
			{
				$is_target 	   = $data[1]['is_target'] ; 
				$target_score  = $data[1]['target_score'] ; 
				$target_overs  = $data[1]['target_overs'] ; 
				
				$batting_team_score  = $data[1]['total_runs'] ; 
				$bowilng_team_score  = $data[0]['total_runs'] ; 
				
				if($is_target == 0 ){					
						$score_diff = $bowilng_team_score - $batting_team_score; 
						if ( $score_diff > 0 ) // 2nd team need runs to win
						{
							$score_need_win = $data[1]['team_name']." need ".$score_diff. " runs to win";		
						}else{	// may be this code is not needed here
							$remaining_wickets = abs(10 - $data[1]['total_wickets']);
							$score_need_win	   = $data[1]['team_name']." won by ".$remaining_wickets. " runs";		
						}
				}else{
						$total_balls  = $data[1]['total_balls'] ; 
						$target_overs  = $this->formatBall(($target_overs * 6 )  - $total_balls) ;
						//$target_overs  = $target_overs - 
						$score_diff =   $target_score - $batting_team_score; 
						if ( $score_diff > 0 ) // 2nd team need runs to win
						{
							$score_need_win = $data[1]['team_name']." need ".$score_diff. " runs to win in ".$target_overs." overs ";		
						}else{	// may be this code is not needed here
							$remaining_wickets = abs(10 - $data[1]['total_wickets']);
							$score_need_win	   = $data[1]['team_name']." won by ".$remaining_wickets. " runs";		
						}
					
				}
			}else{
				$is_target 	   = $data[0]['is_target'] ; 
				$target_overs  = $data[0]['target_overs']; 
				if($is_target == 1 ){
					$score_need_win = 'Match is reduced to '.$target_overs.' overs per side';
				}
				
			}
		}
 
  			if(sizeof($data) > 0 ){
 				foreach($data as &$inning_info){
  						$total_balls 	=  $inning_info['total_balls'];
						$inning_info['total_balls']  =  floor($total_balls/6).".".($total_balls%6) ; 
  				}//foreach
 			}//if 
			
 			
			$match_info = $this->getMatchInfo($match_id);
			
			$match_result  = $match_info['match_result'];
			$match_summary = $match_info['match_summary'];
			$match_status  = $match_info['match_status'];

			stemplate::assign("match_result", $match_result);
			stemplate::assign("match_summary", $match_summary);
			stemplate::assign("match_status", $match_status);
			
 			$total_inning = sizeof($data); 
			
			stemplate::assign("total_inning", sizeof($data));
			stemplate::assign("inning_info", $data);
			stemplate::assign("score_need_win", $score_need_win);
			stemplate::assign("toss_decision_string", $toss_decision_string);
					
 			stemplate::display("live_match_widget/match_inning_stat.tpl");
 
 	}//function 
	
	/*function getLeftScoreBoardWidget( $post  ,$match_inning_info){
			
			$match_id = $post['match_id'];
			$match_inning_info = array_reverse( $match_inning_info );
			
			$team_name 	   = $match_inning_info[0]['team_name'];
			$total_runs    = $match_inning_info[0]['total_runs'];
			$total_wickets = $match_inning_info[0]['total_wickets'];
			$total_balls   = $this->formatBall($match_inning_info[0]['total_balls']);
			echo $string = '<strong>'.$team_name.' <span class="score">'.$total_runs.'/'.$total_wickets.'</span></strong><span class="score"></span> ('.$total_balls.'ov)';
			
			
	}//getLeftScoreBoardWidget*/


	function getButtomScoreBoardWidget()
	{
		global $db, $config;
		$baseurl = $config['baseurl'];
			
 				
		$sql = "select date_format( ts.match_start_date, '%a %b %e %H:%i' ) AS start_date, 
				match_inning_id,pcb_match_inning.match_id,batting_team_id,bowling_team_id , 
				getTeamNameByTournamentTeamIdl(batting_team_id) as team_name,toss_decision,match_status,
				total_runs , total_wickets , total_balls, getMatchScheduleNameById(pcb_match.tournament_schedule_id) as match_name
				from pcb_match_inning, pcb_match , pcb_tournament_schedule ts
				where pcb_match.tournament_schedule_id = ts.tournament_schedule_id AND pcb_match.cover_as_live = 1 
				AND pcb_match_inning.match_id = pcb_match.match_id and pcb_match.match_result = 'pending' 
				ORDER BY pcb_match.match_id ASC , match_inning_id DESC limit 1 ";		
				
		$match_inning_info = $db->first($sql);
		if( isset($match_inning_info['match_id']) ) // if any live match running
		{
			
			$match_id = $match_inning_info['match_id'];
			$team_name 	   = $match_inning_info['team_name'];
			$total_runs    = $match_inning_info['total_runs'];
			$total_wickets = $match_inning_info['total_wickets'];
			$total_balls   = $this->formatBall($match_inning_info['total_balls']);
			
			$toss_decision = $match_inning_info['toss_decision'];
			$match_status  = $match_inning_info['match_status'];
			
			if($toss_decision == "pending"){
			
				echo '<a href="'.$baseurl.'/live_match.php?match_id='.$match_id.'">';
				echo $string = '<p> '.$team_name.'&nbsp;<span>'.$match_status.'</span></a></p>';				
			
			}else{
				
				echo '<a href="'.$baseurl.'/live_match.php?match_id='.$match_id.'">';
				echo $string = '<p> '.$team_name.'&nbsp;<span>'.$total_runs.'/'.$total_wickets.'</span>&nbsp;('.$total_balls.'ov)</p>';
				echo '</a>';	
							
			}
			
		}
		else	// other wise show match result latest match result and link to fixture/result page
		{
			$sql = "select match_id, getMatchScheduleNameById(pcb_match.tournament_schedule_id) as match_name, match_summary 
					from pcb_match 
					where pcb_match.cover_as_live = 1 AND match_result != 'pending' order by match_id desc limit 1";
			$match_info = $db->first($sql);
			$match_id = $match_info['match_id'];
			$match_name = $match_info['match_name'];
			
			if( isset($match_info['match_summary']) ){
				echo '<a href="'.$baseurl.'/live_match.php?match_id='.$match_id.'">';
				echo '<p>'.$match_info['match_name'].':'.$match_info['match_summary'].'</p>';
				echo '</a>';				
			}
		}
			
	}//getLeftScoreBoardWidget
 	

	
	function getLeftScoreBoardWidget()
	{
		//tournament_id = 8 is demo tournament		
		global $db, $config;
		$baseurl = $config['baseurl'];
			
		$sql = "select match_inning_id,pcb_match_inning.match_id,batting_team_id,bowling_team_id , 
				getTeamNameByTournamentTeamIdl(batting_team_id) as team_name,toss_decision,match_status,
				total_runs , total_wickets , total_balls, getMatchScheduleNameById(pcb_match.tournament_schedule_id) as match_name
				from pcb_match_inning, pcb_match 
				where pcb_match.cover_as_live = 1 AND pcb_match_inning.match_id = pcb_match.match_id and pcb_match.match_result = 'pending' AND pcb_match.tournament_id != 8
				order by match_inning_id desc limit 1";
				
		$match_inning_info = $db->first($sql);
		if( isset($match_inning_info['match_id']) ) // if any live match running
		{
			
			$match_id = $match_inning_info['match_id'];
			$team_name 	   = $match_inning_info['team_name'];
			$total_runs    = $match_inning_info['total_runs'];
			$total_wickets = $match_inning_info['total_wickets'];
			$total_balls   = $this->formatBall($match_inning_info['total_balls']);
			
			$toss_decision = $match_inning_info['toss_decision'];
			$match_status  = $match_inning_info['match_status'];
			
			if($toss_decision == "pending"){
			
				echo '<a href="'.$baseurl.'/live_match.php?match_id='.$match_id.'">';
				echo $string = '<strong>'.$match_inning_info['match_name'].' : '.$team_name.'&nbsp;<span class="score">'.$match_status.'</span></strong></a>';				
			
			}else{
				echo '<a href="'.$baseurl.'/live_match.php?match_id='.$match_id.'">';
				echo $string = '<strong>'.$match_inning_info['match_name'].' : '.$team_name.'&nbsp;<span class="score">'.$total_runs.'/'.$total_wickets.'</span></strong>&nbsp;('.$total_balls.'ov)';
				echo '</a>';				
			}
			
		}
		else	// other wise show match result latest match result and link to fixture/result page
		{
			$sql = "select match_id, getMatchScheduleNameById(pcb_match.tournament_schedule_id) as match_name, match_summary 
					from pcb_match 
					where pcb_match.cover_as_live = 1 AND match_result != 'pending' order by match_id desc limit 1";
			$match_info = $db->first($sql);
			$match_id = $match_info['match_id'];
			$match_name = $match_info['match_name'];
			
			if( isset($match_info['match_summary']) ){
				echo '<a href="'.$baseurl.'/live_match.php?match_id='.$match_id.'">';
				echo '<strong>'.$match_info['match_name'].':'.$match_info['match_summary'].'</strong>';
				echo '</a>';				
			}
		}
			
	}//getLeftScoreBoardWidget
 	
	
	function loadPartnership($post , $match_inning_info , $match_info){
			
			//varDump($post);
			
			global $config;
			$type			 = $post['type'];
			$match_id 		 = $post['match_id'];
			$match_type		 =  $post['match_type_code'];
  			$toss_decision		  = $match_info['toss_decision'];
			
			if($toss_decision == 'pending') { return false;}
			
   		    $total_inning = ((int)sizeof($match_inning_info) - 1)  ; 
			
			if($type == 'all_innings'){  
					$match_inning_id   = $match_inning_info[$total_inning]['match_inning_id'];
					$match_id		   = $match_inning_info[$total_inning]['match_id'];
					$batting_team_id   = $match_inning_info[$total_inning]['batting_team_id'];
					$bowling_team_id   = $match_inning_info[$total_inning]['bowling_team_id'];
			}else{ 
					$match_inning_id   = $post['match_inning_id'];
					$match_id		   = $post['match_id'];
					$batting_team_id   = $post['batting_team_id'];
					$bowling_team_id   = $post['bowling_team_id'];
			}

			$file_name = $match_type.'_match_'.$match_id.'_'.$match_inning_id.'_'.$batting_team_id.'_scoreboard.xml';
			$file_path = $config['baseurl'].'/live_comentary_xml/'.$file_name; 
			
			$xml_string = parseXMLFileTOString($file_path);
			
			$doc		= new DOMDocument();
			$doc->loadHTML($xml_string);
			
			$player_partnership = $doc->getElementsByTagName("player_partnership");
			$counter = 0 ; 	
 			foreach($player_partnership as $partnership_data){   
					$partnership_info[$counter]['player_1_name']    = $partnership_data->getAttribute('player_1_name'); 
					$partnership_info[$counter]['player_2_name']    = $partnership_data->getAttribute('player_2_name'); 
					$partnership_info[$counter]['partnership_runs'] = $partnership_data->getAttribute('partnership_runs'); 
					$partnership_info[$counter]['partnership_start']= $partnership_data->getAttribute('partnership_start'); 
					$partnership_info[$counter]['partnership_end']  = $partnership_data->getAttribute('partnership_end'); 
					$partnership_info[$counter]['player_1_score'] 	= $partnership_data->getAttribute('player_1_score'); 
					$partnership_info[$counter]['player_2_score'] 	= $partnership_data->getAttribute('player_2_score'); 
					$partnership_info[$counter]['player_1_balls'] 	= $partnership_data->getAttribute('player_1_balls'); 
					$partnership_info[$counter]['player_2_balls'] 	= $partnership_data->getAttribute('player_2_balls'); 
					$partnership_info[$counter]['balls'] 			= $partnership_data->getAttribute('balls'); 
					$partnership_info[$counter]['overs'] 			= $partnership_data->getAttribute('overs'); 
					$partnership_info[$counter]['wide_balls'] 		= $partnership_data->getAttribute('wide_balls'); 
					$partnership_info[$counter]['bye'] 				= $partnership_data->getAttribute('bye'); 
					$partnership_info[$counter]['leg_bye'] 			= $partnership_data->getAttribute('leg_bye'); 
					$partnership_info[$counter]['no_balls'] 		= $partnership_data->getAttribute('no_balls'); 
					
					$partnership_info[$counter]['total_extra'] 		= ($partnership_data->getAttribute('no_balls')+ $partnership_data->getAttribute('leg_bye') +$partnership_data->getAttribute('bye') +$partnership_data->getAttribute('wide_balls') );  					
					
					$partnership_info[$counter]['total_score'] 		= ($partnership_info[$counter]['total_extra']+ $partnership_info[$counter]['player_1_score'] + $partnership_info[$counter]['player_2_score'] );  					



					$partnership_info[$counter]['p_p_player_1']     = abs((($partnership_data->getAttribute('player_1_score') / $partnership_info[$counter]['total_score'] ) *100) )  ; 


					$partnership_info[$counter]['p_p_player_2']     = abs( (($partnership_data->getAttribute('player_2_score') / $partnership_info[$counter]['total_score'] ) *100)  )  ; 
	
					


					$counter++;
					
					
			}//$batsman
			
					stemplate::assign("match_inning_id", $match_inning_id);
					stemplate::assign("match_inning_info", $match_inning_info);
					stemplate::assign("partnership_info", $partnership_info);
					stemplate::display("live_match_widget/partnership.tpl");
		
	}//loadPartnership
	
	
	function matchClosePlayANDMatchNotes($match_id){
		global $config ; 
		
		$file_name_ = 'match_notes_'.$match_id.'.xml';
  		$file_path_ = $config['baseurl'].'/live_comentary_xml/'.$file_name_; 
 		$xml_string_ = parseXMLFileTOString($file_path_);
		$doc_		= new DOMDocument();
	    $doc_->loadHTML( $xml_string_ );
		
		$day_result = $doc_->getElementsByTagName("day_result");	
		$match_close_play = array();
		$counter = 0 ; 
		foreach($day_result as $day_result_info){   
				
				$match_close_play[$counter]['match_inning_id']   =  $day_result_info->getAttribute('match_inning_id'); 
				$match_close_play[$counter]['day'] 			     =  $day_result_info->getAttribute('day'); 
				$match_close_play[$counter]['batting_team_name'] =  $day_result_info->getAttribute('batting_team_name'); 
				$match_close_play[$counter]['total_score']       =  $day_result_info->getAttribute('total_score'); 
				$match_close_play[$counter]['total_wickets'] 	 =  $day_result_info->getAttribute('total_wickets'); 
				$match_close_play[$counter]['total_over'] 	     =  $this->formatBall($day_result_info->getAttribute('total_over')); 
				$match_close_play[$counter]['player_1_name'] 	 =  $day_result_info->getAttribute('player_1_name'); 
				$match_close_play[$counter]['player_1_score']    =  $day_result_info->getAttribute('player_1_score'); 
				$match_close_play[$counter]['player_2_name'] 	 =  $day_result_info->getAttribute('player_2_name'); 
				$match_close_play[$counter]['player_2_score']    =  $day_result_info->getAttribute('player_2_score'); 
				$match_close_play[$counter]['inning_no']    	 =  $day_result_info->getAttribute('inning_no');
				$match_close_play[$counter]['play_status']    	 =  $day_result_info->getAttribute('play_status');
				$match_close_play[$counter]['is_toss']	    	 =  $day_result_info->getAttribute('is_toss');
				$match_close_play[$counter]['match_status_incase_issue']  =  $day_result_info->getAttribute('match_status_incase_issue');
				$counter++;
				
 		}
		
 		stemplate::assign("match_close_play", $match_close_play);
		
	}//matchClosePlayANDMatchNotes
	
	function getMatchNotes( $post ){
 		global $config , $db ; 	
		$match_id    = $post['match_id'];
		$match_type  = $post['match_type'];
		
		$sql = "select match_id , match_inning_id , getTeamNameByTournamentTeamIdl(batting_team_id) as team_name , batting_team_id , date_format(created_on,'%Y-%m-%d') as created_on
				from pcb_match_notes
				where match_id = '".$match_id."'";
		if($match_type =="TEST"){		
			$sql .= "group by date_format(created_on,'%Y-%m-%d')";
		}else{
			$sql .= "group by match_inning_id";
			
		}
		$sql .= " order by match_notes_id asc";		

		$result = $db->fetch_all($sql);
 		foreach($result as &$row){
 			$match_inning_id = $row['match_inning_id'];
			$created_on = $row['created_on'];
			
	 	    $sql_ = "select match_id , match_inning_id , getTeamNameByTournamentTeamIdl(batting_team_id) as team_name , batting_team_id ,notes_type,
					 total_runs , total_balls , total_wickets , player_1_id , player_2_id , 
					 if(player_1_id > 0, getPlayerNameById(player_1_id), 0) as player_1_name,
					 if(player_2_id > 0, getPlayerNameById(player_2_id), 0) as player_2_name,
					 player_1_runs , player_2_runs , total_extra , total_four , total_six , partnership_count
					 from pcb_match_notes
					 where match_id = '".$match_id."' ";;

			if($match_type =="TEST"){		
				$sql_ .= "AND date_format(created_on,'%Y-%m-%d') = '".$created_on."'";
			}else{
				$sql_ .= "AND  match_inning_id = '".$match_inning_id."' ";	
			}
 
			$sql_ .=" order by match_notes_id asc";		
					 
			$result_ = $db->fetch_all( $sql_ );
			foreach($result_ as &$row_){
				$row_['total_overs'] =  $this->formatBall($row_['total_balls']);	
			}
			$row[$match_inning_id] = $result_ ; 
			
		}// outer foreach 
		// varDump($result);
		
		stemplate::assign("match_type", $match_type);
		stemplate::assign("live_match_notes", $result);
		stemplate::display("live_match_widget/live_match_notes.tpl");
		 			
	}//getMatchNotes
	
}//class
?>